package com.good.shopping.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.good.shopping.product.entity.AttrEntity;
import com.good.shopping.product.service.AttrAttrgroupRelationService;
import com.good.shopping.product.service.AttrService;
import com.good.shopping.product.service.CategoryService;
import com.good.shopping.product.vo.AttrGroupRelatinVo;
import com.good.shopping.product.vo.AttrGroupWithAttrsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.good.shopping.product.entity.AttrGroupEntity;
import com.good.shopping.product.service.AttrGroupService;
import com.good.common.utils.PageUtils;
import com.good.common.utils.R;



/**
 * 属性分组
 *
 * @author yan
 * @email 821521880@qq.com
 * @date 2022-01-29 11:46:23
 */
@Api(value = "属性分组", tags = {"属性分组"})
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    // 属性分组
    @Autowired
    private AttrGroupService attrGroupService;

    // 商品三级分类
    @Autowired
    private CategoryService categoryService;

    // 商品属性
    @Autowired
    private AttrService attrService;

    // 属性&属性分组关联
    @Autowired
    AttrAttrgroupRelationService relationService;

    /**
     * 列表
     *    参数：根据三级分类id  0 代表查询全部
     */
    @ApiOperation(value = "列表查询")
    @GetMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     *  属性分组 - 关联 -列表查询
     *     attrgroupId  分组id
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 属性分组 - 关联 - 批量删除
     *   AttrGroupRelatinVo 数组 或者list 都可以
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelatinVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 获取当前分组没有关联的所有属性
     * 平台属性 - 属性分组 - 关联 - 新建关联 - 查询列表
     * @param attrgroupId 分组id
     * @param params 分页信息
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     * 平台属性 - 属性分组 - 关联 - 新建关联 - 确认新增
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelatinVo> vos) {
        relationService.saveBatch(vos);
        return R.ok();
    }


    /**
     * 商品维护 - 发布商品 -规格参数 - 查询某个分类的下属性列表
     *    获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        // 1 查出当前分类下的所有属性分组
        // 2 查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

}
